package fr.p10.miage.m1.lehouarner.car1.td2;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class BonjourServlet
 */
@WebServlet("/BonjourServlet")
public class BonjourServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Authentification aut;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BonjourServlet() {
        super();
        Authentification.chargement();
        aut = new Authentification();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        /* Exercice 1
         * out.println("<h1><strong>Bonjour</strong></h1>"); 
         */
        
        /* Exercice 2
         * if(request.getParameter("login") != null){
		 *      String login = request.getParameter("login");
		 *		String password = request.getParameter("password");
		 *		out.println("<html><body>");
		 *		out.println("Login : " + login + " | Mot de passe : " + password);
		 *       out.println("</body></html>");
         *	}
         */
         out.println("<html><body>");
         out.println("<form method=\"post\" action=\"http://localhost:8080/fr.p10.miage.m1.lehouarner.car1.td2/BonjourServlet\">");
         //Exercice 2
         //out.println("<form method=\"get\" action=\"http://localhost:8080/fr.p10.miage.m1.lehouarner.car1.td2/BonjourServlet\">");
         out.println("<p>");
         out.println("Login : <input type=\"text\" name=\"login\"/><br/>");
         out.println("Password : <input type=\"text\" name=\"password\"/>");
         out.println("</p><br/>");
         out.println("<input type=\"submit\" /> <input type=\"reset\"/>");
         out.println("</form>");
         out.println("</body></html>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
        PrintWriter out = response.getWriter();
		String login = request.getParameter("login");
		String password = request.getParameter("password");
		out.println("<html><body>");
		if(aut.estReconnue(login, password))
			out.println("Utilisateur reconnu");
		else
			out.println("Utilisateur non reconnu");
        out.println("</body></html>");
	}

}
